//
//  YLMineMoneyViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/12/4.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMineMoneyViewModel.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "WHProgressHUD.h"
#import "YLMineMoneyModel.h"
#import <YYModel.h>
@implementation YLMineMoneyViewModel

- (RACCommand *)investmentCommand
{
    if (!_investmentCommand) {
        __weak typeof(self) weakSelf = self;
        _investmentCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
           
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetInvestmentList", YLURL];
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:nil success:^(id responseObject) {
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        
                        [weakSelf.models removeAllObjects];
                        
                        YLMineMoneyItem *item = [YLMineMoneyItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                        
                        for (YLMineMoneyModel *model in item.item) {
                            // 在投金额
                            model.type = YLMineMoneyModelTypeInvestment;
                            [weakSelf.models addObject:model];
                        }
                        
                        [subscriber sendNext:nil];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                        [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    }
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    
                    
                    [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    [subscriber sendCompleted];
                    
                }];
               
                return nil;
            }];
        }];
    }
    return _investmentCommand;
}

- (RACCommand *)profitCommand
{
    if (!_profitCommand) {
        __weak typeof(self) weakSelf = self;
        _profitCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSString *url = [NSString stringWithFormat:@"%@%@", YLURL,weakSelf.urlStr];
                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
                NSInteger index = [input integerValue];
            
                params[@"PageIndex"] = @(index);
                NSInteger count = 10;
                params[@"MaxResultCount"] = @(count);
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        YLMineMoneyItem *item = [YLMineMoneyItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                        
                        if (index == 1) {
                            [weakSelf.models removeAllObjects];
                        }
                        
                        for (YLMineMoneyModel *model in item.item) {
                            // 在投金额
                            model.type = weakSelf.type;
                            [weakSelf.models addObject:model];
                        }
                        
                        [subscriber sendNext:nil];
                        
                        if (item.item.count < count) {
                            [weakSelf.wh_noMoreDataSubject sendNext:nil];
                        }
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                        [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    }
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
            
            
        }];
        
    }
    return _profitCommand;
}

- (NSMutableArray *)models
{
    if (!_models) {
        _models = [NSMutableArray array];
    }
    return _models;
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
}

@end
